package com.example.coreshopapp.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.coreshopapp.R;
import com.example.coreshopapp.bean.GoodsData;
import com.example.coreshopapp.fragment.BottomShopCartFragment;

import java.util.List;

import bean.OrderItemBean;

public class ShopCartBottomAdapter extends RecyclerView.Adapter<ShopCartBottomAdapter.ViewHolder> {
    private List<OrderItemBean> shopCartList;

    public ShopCartBottomAdapter(List<OrderItemBean> shopCartList) {
        this.shopCartList = shopCartList;
    }
    public void update(List<OrderItemBean> shopCartList) {
        this.shopCartList = shopCartList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View view = View.inflate(parent.getContext(), R.layout.shop_cart_bottom_item, null);
        View view = LayoutInflater.from(parent.getContext()).inflate( R.layout.shop_cart_bottom_item, parent,false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        OrderItemBean item = shopCartList.get(position);
        holder.cartQuantityTv.setText(item.getNums() + "");

        if(item.getUnit() != null) {
          holder.cartSpecTv.setText(item.getUnit());
        }  else {
            holder.cartSpecTv.setVisibility(View.GONE);
        }

        if(item.getName() != null) {
          holder.cartNameTv.setText(item.getName());
        } else {
            holder.cartSpecTv.setText("暂无");
        }
        holder.cartPriceTv.setText((item.getPromotionPrice() > 0 ? item.getPromotionPrice()  : item.getPrice()) + "");
        holder.costPriceTv.setText(item.getPromotionPrice() > 0 ? item.getPrice() + "" : "");
        holder.costPriceLy.setVisibility(item.getPromotionPrice() > 0 ? View.VISIBLE  : View.GONE);

        holder.addBtn.setOnClickListener(view -> {
            if (setOnAddItemClickListener != null) {
                setOnAddItemClickListener.onAddClick(item,position);
            }
        });

        holder.minusBtn.setOnClickListener(view -> {
            if (setOnMinusItemClickListener !=null) {
                setOnMinusItemClickListener.onMinusClick(item,position);
            }
        });
        // 跳转到操作界面带参数
        holder.operationBtn.setOnClickListener(view -> {
            if(operationBtnItemClickListener != null) {
                operationBtnItemClickListener.onItemClick(item,position);
            }
        });

        // 点击数量，弹出弹窗，输入数字
        holder.cartQuantityTv.setOnClickListener(view -> {
            if (onItemNumClickListener != null) {
                        onItemNumClickListener.onItemAddClick(item);
            }
        });
    }

    @Override
    public int getItemCount() {
        return shopCartList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView cartNameTv,cartQuantityTv,cartPriceTv,cartSpecTv,addBtn,minusBtn,operationBtn,costPriceTv;
        private LinearLayout costPriceLy;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cartNameTv = itemView.findViewById(R.id.cart_name_tv);
            cartPriceTv = itemView.findViewById(R.id.cart_price_tv);
            cartQuantityTv = itemView.findViewById(R.id.cart_quantity_tv);
            cartSpecTv = itemView.findViewById(R.id.cart_spec_tv);
            addBtn = itemView.findViewById(R.id.add_btn);
            minusBtn = itemView.findViewById(R.id.minus_btn);
            operationBtn = itemView.findViewById(R.id.goods_operation);
            costPriceLy = itemView.findViewById(R.id.cost_price_ly);
            costPriceTv = itemView.findViewById(R.id.cost_price_tv);
        }
    }
    // 添加
    public interface SetOnAddItemClickListener {
        void onAddClick(OrderItemBean item,int i);
    }
    private SetOnAddItemClickListener setOnAddItemClickListener;

    public  void onAddItemClickListener(SetOnAddItemClickListener newClick) {
        setOnAddItemClickListener = newClick;
    }
    // 减少
    public interface SetOnMinusItemClickListener {
        void onMinusClick(OrderItemBean item,int i);
    }
    private SetOnMinusItemClickListener setOnMinusItemClickListener;

    public void onMinusItemClickListener(SetOnMinusItemClickListener newClick) {
        setOnMinusItemClickListener = newClick;
    }

   public interface OperationBtnItemClickListener {
        void onItemClick(OrderItemBean orderItem,int i); // 数据
    }
    private  OperationBtnItemClickListener operationBtnItemClickListener;
    public void setOperationBtnItemClickListener(ShopCartBottomAdapter.OperationBtnItemClickListener newClick) {
        operationBtnItemClickListener = newClick;
    }

    private ShopCartBottomAdapter.OnItemNumClickListener onItemNumClickListener;

    public interface OnItemNumClickListener {
        void onItemAddClick(OrderItemBean orderItem);
    }
    public void setOnItemNumClickListener(ShopCartBottomAdapter.OnItemNumClickListener onMyItemNumClickListener) {
        onItemNumClickListener = onMyItemNumClickListener;
    }
}
